﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using Model;
using System.Configuration;
using System.Data;

namespace TCDC_System.Attribute.xml
{
    /// <summary>
    /// Summary description for Campaign
    /// </summary>
    public class Campaign : IHttpHandler
    {

        private DBUtil DB;
        XmlTextWriter objX;

        public void ProcessRequest(HttpContext context)
        {
            string type_id = context.Request.QueryString["type"];
            string group_id = context.Request.QueryString["group"];

            this.DB = new DBUtil();
            DB.strConn = ConfigurationManager.AppSettings["ConnString"];

            string sql = "SELECT * FROM " +
                         "tcdc_mst_campaign c, " + 
                         "tcdc_mst_campaign_detail d, " +
                         "tcdc_mst_campaign_name n " +
                         "WHERE c.campaign_detail_id = d.campaign_detail_id " +
                         "AND d.campaign_name_id = n.campaign_name_id " +
                         "AND d.member_type_id = '" + type_id + "' " +
                         "AND d.campaign_group_id = '" + group_id + "' ";

            DataTable dt = DB.ExecuteReader(sql);

            context.Response.Clear();
            context.Response.ContentType = "text/xml";

            objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("campaigns");

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {

                    objX.WriteStartElement("campaign");
                    objX.WriteElementString("campaign_id", row["campaign_id"].ToString());
                    objX.WriteElementString("campaign_name_th", row["campaign_name_th"].ToString());
                    objX.WriteElementString("campaign_name_en", row["campaign_name_en"].ToString());
                    objX.WriteElementString("campaign_month", row["campaign_month"].ToString());
                    objX.WriteElementString("campaign_year", row["campaign_year"].ToString());
                    objX.WriteElementString("campaign_price", row["campaign_price"].ToString());

                    string display_value_th = row["campaign_name_th"].ToString() + 
                                              " : ราคา " + string.Format("{0:##,###.00}",Convert.ToInt32(row["campaign_price"])) + " บาท";
                    objX.WriteElementString("display_value_th", display_value_th);
                    objX.WriteEndElement();
                }
            }
            else
            {
                objX.WriteStartElement("campaign");
                objX.WriteElementString("campaign_id", "");
                objX.WriteElementString("campaign_name_th", "");
                objX.WriteElementString("campaign_name_en", "");
                objX.WriteElementString("campaign_month", "");
                objX.WriteElementString("campaign_year", "");
                objX.WriteElementString("campaign_price", "");
                objX.WriteElementString("display_value_th", "");
                    
                objX.WriteEndElement();
            }

            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}